home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / sonido / mod-0.000 / mod-0 / mod / load_ult.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-15  |  11.2 KB  |  498 lines

  1. /*
  2.  *  load_ult.c - Loads Ultratracker modules.
  3.  *
  4.  *  (C) 1994 Mikael Nordqvist (d91mn@efd.lth.se, mech@df.lth.se)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <sys/types.h>
  11. #include <fcntl.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/soundcard.h>
  14. #include <sys/ultrasound.h>
  15. #include <limits.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18.  
  19. #include "mod.h"
  20.  
  21. /* External global variables */
  22.  
  23. SEQ_DECLAREBUF();
  24. extern int seqfd, gus_dev;
  25.  
  26. extern struct mod_info M;
  27. extern struct options opt;
  28.  
  29. extern char quit;
  30.  
  31. extern char effect_used[NR_EFX];
  32.  
  33. /* Local functions */
  34. static int process_header(int);
  35. static int read_patterndata(int);
  36. static void fix_effect(unsigned char *, unsigned char *);
  37.  
  38. /* Defines and variables used while loading module */
  39.  
  40. #define ULT_OFFSET_MAGIC         0
  41. #define ULT_OFFSET_NAME         15
  42. #define ULT_OFFSET_SONGTEXTLEN  47
  43. #define ULT_OFFSET_SONGTEXT     48
  44.  
  45. #define ULT_SAMPLEINFO_SIZE     64
  46. #define ULT_SAMPLEINFO_SIZE_NEW 66
  47.  
  48. #define ULT_FLAG_BITS_16         4
  49. #define ULT_FLAG_LOOP            8
  50. #define ULT_FLAG_LOOPBACKWARD   16
  51.  
  52. #define ULT_REPEATBLOCK_NOTE 0xfc
  53.  
  54. static int expected_length, version;
  55.  
  56. int load_ult(int fd)
  57. {
  58.     char tmpbuf[80];
  59.  
  60.     int i, diff, fatalerr;
  61.  
  62.     print_status("Loading ULT");
  63.     M.nr_tracks=0;
  64.     if(!process_header(fd))
  65.     return 0;
  66.  
  67.     if(!read_patterndata(fd)) {
  68.     info("Unable to read patterndata.\n");
  69.     return 0;
  70.     }
  71.     
  72.     ioctl(seqfd, SNDCTL_SEQ_RESETSAMPLES, &gus_dev);
  73.     
  74.     info("\nUploading instruments to GUS...\n");
  75.     
  76.     fatalerr=0;
  77.     for(i=1; i <= M.nr_samples; ++i) {
  78.     if(M.sample[i].length > 4) { /* Only load non-silent samples */
  79.         if(!read_and_upload_sample(fd, i)) {
  80.         fatalerr=1;
  81.         break;
  82.         }
  83.     }
  84.     else { /* Skip sample */
  85.         if(lseek(fd, M.sample[i].length, SEEK_CUR) == -1){
  86.         fatalerr=1;
  87.         break;
  88.         }
  89.     }
  90.     if(quit == QUIT_SIGNAL) /* Bail out if we got QUIT_SIGNAL */
  91.         return 0;
  92.     }
  93.     
  94.     if(fatalerr) {
  95.     diff=expected_length-lseek(fd, 0, SEEK_CUR);
  96.     sprintf(tmpbuf, "File %d bytes short", diff);
  97.     print_status(tmpbuf);
  98.     info("\nShort file (%d bytes missing).\n",0);
  99.     sleep(1);
  100.     return 0;
  101.     }
  102.     
  103.     /* Check if filelength was as expected. This is done with two 
  104.      * statements to make sure we don't move the filepointer to the 
  105.      * end before checking current position.
  106.      */
  107.  
  108.     diff=-lseek(fd, 0, SEEK_CUR);
  109.     diff+=lseek(fd, 0, SEEK_END);
  110.  
  111.     if(diff && opt.verbose) {
  112.     warning("\nFound %d garbagebytes after samples. Ignoring.\n", diff);
  113.     }
  114.     
  115.     print_songname(M.name);
  116.     print_samples(1);
  117.     print_songinfo(M.nr_voices, M.nr_samples, "ULT", expected_length,
  118.            M.songlength, M.nr_patterns);
  119.     return 1;
  120. }
  121.  
  122.  
  123. static int process_header(int fd)
  124. {
  125.     unsigned char buf[32*255+1]; /* Maximum size of SONGTEXT */
  126.     char *p;
  127.     int tmp, i, current_si_size;
  128.     struct sample_info *s;
  129.  
  130.     if(read(fd, buf, ULT_OFFSET_SONGTEXT) != ULT_OFFSET_SONGTEXT) {
  131.     info("Unable to read header from file.\n");
  132.     return 0;
  133.     }
  134.     
  135.     expected_length=ULT_OFFSET_SONGTEXT; /* Start counting expected filesize */
  136.     
  137.     if(strncmp("MAS_UTrack_V", &buf[ULT_OFFSET_MAGIC], 12)) {
  138.     info("Not an Ultratracker module.\n");
  139.     return 0;
  140.     }
  141.     
  142.     /* Copy songname and strip trailing spaces */
  143.     strncpy(M.name, &buf[ULT_OFFSET_NAME], 32);
  144.     M.name[32]=0;
  145.     fix_string(M.name);
  146.     
  147.     buf[ULT_OFFSET_MAGIC+15]=0;
  148.     version=atoi(&buf[ULT_OFFSET_MAGIC+12]);
  149.     if(version < 1 || version >4) {
  150.     print_status("Format not supported");
  151.     info("Format version '%s' not supported.\n", &buf[ULT_OFFSET_MAGIC]);
  152.     return 0;
  153.     }
  154.  
  155.     info("Format version '%s'.\n", &buf[ULT_OFFSET_MAGIC]);
  156.     
  157.     i=32*buf[ULT_OFFSET_SONGTEXTLEN];
  158.     if(version >= 2 && i) {
  159.     M.songtext=(void *)malloc(i+1);
  160.     if(read(fd, M.songtext, i) != i) {
  161.         info("Unable to load songtext.\n");
  162.         return 0;
  163.     }
  164.     *(M.songtext+i)=0;
  165.     fix_string(M.songtext);
  166.     expected_length+=i;
  167.     }
  168.     else
  169.     M.songtext=0;
  170.     
  171.     if(M.songtext && opt.verbose) {
  172.     printf("\n---------- Message: ----------\n");
  173.     i=strlen(M.songtext);
  174.     tmp=0;
  175.     while(tmp < i) {
  176.         printf("%.32s\n", &M.songtext[tmp]);
  177.         tmp+=32;
  178.     }
  179.     printf("\n------------------------------\n");
  180.     }
  181.     
  182.     if(read(fd, buf, 1) != 1) {
  183.     info("Unable to read header from file.\n");
  184.     return 0;
  185.     }
  186.     M.nr_samples=(unsigned char)buf[0];
  187.     expected_length++;
  188.     
  189.     if(!M.nr_samples || M.nr_samples > 127) {
  190.     info("Invalid number of samples in module.\n");
  191.     return 0;
  192.     }
  193.  
  194.     if(version >= 2) { /* Linear volumes */
  195.     }
  196.     else {             /* LOG volumes */
  197.     info("Logarithmic volumes not supported (yet). Might sound"
  198.          " a bit strange.\n");
  199.     }
  200.     
  201.     M.sample=(struct sample_info *)malloc((1+M.nr_samples)*
  202.                       sizeof(struct sample_info));
  203.     
  204.     current_si_size=(version < 4 ? ULT_SAMPLEINFO_SIZE : 
  205.              ULT_SAMPLEINFO_SIZE_NEW);
  206.  
  207.     expected_length+=M.nr_samples*current_si_size;
  208.     print_sample_info_header();
  209.  
  210.     for(i=1; i <= M.nr_samples; ++i) {
  211.     if(read(fd, buf, current_si_size) != current_si_size) {
  212.         info("Unable to read sampleheader (%d).\n", i);
  213.         return 0;
  214.     }
  215.     p=buf;
  216.  
  217.     s=&M.sample[i];
  218.         s->valid=0;             /* Valid isn't set to TRUE until it's
  219.                  * sampledata has been read.
  220.                  */
  221.     strncpy(s->name, p, 32);
  222.     s->name[32]=0;
  223.         p+=32;
  224.         fix_string(s->name);
  225.     
  226.     strncpy(s->dosname, p, 12);
  227.     s->dosname[12]=0;
  228.         p+=12;
  229.         fix_string(s->dosname);
  230.     
  231.     s->repeat_start=*(unsigned long *)p; p+=4;
  232.     s->repeat_end=*(unsigned long *)p;   p+=4;
  233.     tmp=*(unsigned long *)p;             p+=4;
  234.     s->length=*(unsigned long *)p-tmp;   p+=4;
  235.     
  236.     s->volume=*(unsigned char *)p++;
  237.     tmp=*(unsigned char *)p++; /* Flags */
  238.  
  239.     if(tmp&ULT_FLAG_BITS_16) {
  240.         s->length*=2;
  241.         s->bits_16=1;
  242.     }
  243.     else
  244.         s->bits_16=0;
  245.  
  246.     s->unsigned_data=0;
  247.     
  248.     if(tmp&ULT_FLAG_LOOP) {
  249.         if(tmp&ULT_FLAG_LOOPBACKWARD)
  250.         s->looped=LOOP_BIDIR;
  251.         else
  252.         s->looped=LOOP_FORWARD;
  253.     }
  254.     else
  255.         s->looped=0;
  256.     
  257.     /* The docs say finetune comes before c2freq, but that isn't
  258.      * the case with the included "cybocult.ult" so...
  259.      */
  260.     if(version >= 4) {
  261.         s->c2freq=*(short *)p;
  262.         p+=2;
  263.     }
  264.     else
  265.         s->c2freq=-1; /* Use PAL/NTSC */
  266.  
  267.     s->finetune=(*(short *)p)/4096; /* Approximate FT */
  268.     p+=2;
  269.  
  270.     s->repeat_start=
  271.         MIN(MAX(0, s->repeat_start), s->length-2);
  272.     
  273.     /* Is this one including/excluding the last sample? */
  274.     s->repeat_end=
  275.         MIN(MAX(0, s->repeat_end), s->length-2);
  276.  
  277.     expected_length+=s->length;
  278.     
  279.     print_sample_info(i);
  280.     }
  281.  
  282.     if(read(fd, buf, 258) != 258) {
  283.     info("Unable to load patterntable.\n");
  284.     return 0;
  285.     }
  286.     expected_length+=258;
  287.  
  288.     memcpy(M.patterntable, buf, 256);
  289.     M.nr_voices=buf[256]+1;
  290.     M.nr_patterns=buf[257]+1;
  291.  
  292.     for(i=0; i < 256; ++i)
  293.     if(M.patterntable[i] >= M.nr_patterns)
  294.         break;
  295.     M.songlength=i;
  296.  
  297.     if(M.nr_voices > 32 || M.nr_voices <= 0) {
  298.     info("Illegal number of voices (%d).\n", M.nr_voices);
  299.     return 0;
  300.     }
  301.     
  302.     if(version >= 3) {
  303.     if(read(fd, M.panning, M.nr_voices) != M.nr_voices) {
  304.         info("Unable to load PAN-position table.\n");
  305.         return 0;
  306.     }
  307.     for(tmp=0; tmp < M.nr_voices; ++tmp)
  308.         M.panning[tmp]&=0x0f;
  309.     expected_length+=M.nr_voices;
  310.     }
  311.     else {
  312.     for(tmp=0; tmp < M.nr_voices; ++tmp)
  313.         M.panning[tmp]=get_voice_balance(tmp);
  314.     }
  315.     
  316.     M.restartpos=0;
  317.     M.volrange=255;
  318.     
  319.     info("\nVoices: %d  Patterns: %d  Songlength: %d\n",
  320.      M.nr_voices, M.nr_patterns, M.songlength);
  321.     
  322.     return 1;
  323. }
  324.  
  325.  
  326. static int read_patterndata(int fd)
  327. {
  328.     unsigned char buf[7];
  329.     unsigned short effect, arg;
  330.     struct event *e;
  331.     int p, l, v, repeat;
  332.     struct event events[64];
  333.     
  334.     for(v=0; v < NR_EFX; ++v)
  335.     effect_used[v]=0;
  336.  
  337.     for(v=0; v < M.nr_voices; ++v)
  338.     M.track_idx[v]=(int *)malloc(M.nr_patterns*sizeof(int));
  339.  
  340.     /* Allocate for worst case (no identical tracks) */
  341.     M.tracks=(struct event **)malloc(M.nr_patterns*M.nr_voices*
  342.                     sizeof(struct event *));
  343.  
  344.     if(!get_bytes(fd, 0, 0))
  345.     return 0;
  346.     
  347.     for(v=0; v < M.nr_voices; ++v) {
  348.     for(p=0; p < M.nr_patterns; ++p) {
  349.         l=0;
  350.         while(l < 64) {
  351.         if(!get_bytes(fd, buf, 5))
  352.             return 0;
  353.         expected_length+=5;
  354.         if(buf[0] == ULT_REPEATBLOCK_NOTE) {
  355.             repeat=buf[1];
  356.             buf[0]=buf[2];
  357.             buf[1]=buf[3];
  358.             buf[2]=buf[4];
  359.             if(!get_bytes(fd, &buf[3], 2))
  360.             return 0;
  361.             expected_length+=2;
  362.         }
  363.         else
  364.             repeat=1;
  365.  
  366.         if(!repeat)
  367.             info("Repeat block with length 0 detected in "
  368.              "pattern %d.\n", p);
  369.         
  370.         while(repeat > 0 && l < 64) {
  371.             e=&events[l];
  372.             e->note=
  373.             (buf[0] ? buf[0]+BASE_NOTE + 2*12: 0); /* X-2 => X-4 */
  374.             e->sample=buf[1];
  375.             arg=*((unsigned short *)&buf[3]);
  376.  
  377.             effect=(buf[2]>>4)&0x0f;    /* First effect */
  378.  
  379.             if(effect != 0x0e) {
  380.             e->arg=(arg>>8)&0xff;
  381.             }
  382.             else {
  383.             effect=0x10|((arg>>12)&0x0f);
  384.             e->arg=(arg>>8)&0x0f;
  385.             }
  386.             e->effect=effect;
  387.             fix_effect(&e->effect, &e->arg);
  388.             
  389.             if(e->effect || e->arg)
  390.             effect_used[e->effect]=1;
  391.             
  392.             effect=buf[2]&0x0f;        /* Second effect */
  393.  
  394.             if(effect != 0x0e) {
  395.             e->arg2=arg&0xff;
  396.             }
  397.             else {
  398.             effect=0x10|((arg>>4)&0x0f);
  399.             e->arg2=arg&0x0f;
  400.             }
  401.             e->effect2=effect;
  402.             fix_effect(&e->effect2, &e->arg2);
  403.             
  404.             if(e->effect2 || e->arg2)
  405.             effect_used[e->effect2]=1;
  406.             
  407.             --repeat;
  408.             ++l;
  409.         }
  410.         if(repeat)
  411.             info("Repeat block reached outside pattern %d.\n", p);
  412.         }
  413.         M.track_idx[v][p]=get_track_idx(events);
  414.     }
  415.     }
  416.     get_bytes(fd, 0, -1);
  417.  
  418.     /* Octaves 2-9 (0-7) allowed */
  419.     opt.low_note=BASE_NOTE+2*12;
  420.     opt.high_note=BASE_NOTE+10*12-1;
  421.  
  422.     print_used_effects();
  423.  
  424.     return 1;
  425. }
  426.  
  427. /* Converts the Ultratracker effects to those used by the playrouting (a
  428.  * set of effects containing all effects in the supported module-formats).
  429.  */
  430.  
  431. static void fix_effect(unsigned char *effect, unsigned char *arg)
  432. {
  433.     switch(*effect) {
  434.       case EFX_PORTAUP:           /* Effects compatible with PT */
  435.       case EFX_PORTADOWN:
  436.       case EFX_PORTANOTE:
  437.       case EFX_VIBRATO:
  438.       case EFX_VOLSLIDE:
  439.       case EFX_VOLUME:
  440.       case EFX_BREAK:
  441.       case EFX_FINEPORTAUP:
  442.       case EFX_FINEPORTADOWN:
  443.       case EFX_RETRIGGER:
  444.       case EFX_FINEVOLSLIDEUP:
  445.       case EFX_FINEVOLSLIDEDOWN:
  446.       case EFX_NOTECUT:
  447.       case EFX_NOTEDELAY:
  448.       case EFX_PTSPEED:
  449.     break;
  450.  
  451.       /* Effects introduced in 1.5 */
  452.       case EFX_ARPEGGIO:
  453.     if(version < 3)
  454.         *effect=*arg=0;
  455.     break;
  456.  
  457.       /* Effects introduced in 1.6 */
  458.       case EFX_TREMOLO:
  459.       case EFX_SAMPLEOFFSET: /* The '99' SO-effect is handled in effects.c */
  460.     if(version < 4)
  461.         *effect=*arg=0;
  462.     else {
  463.         if(*effect == EFX_SAMPLEOFFSET)
  464.         *effect=EFX_SAMPLEOFFSET_ULT;
  465.     }
  466.     break;
  467.       case EFX_UNUSED2:           /* ULT: Patterndelay */
  468.     if(version < 4)
  469.         *effect=*arg=0;
  470.     else
  471.         *effect=EFX_PATTERNDELAY;
  472.     break;
  473.  
  474.     
  475.     /* Special ULT-effects */
  476.       case EFX_JUMP:              /* ULT: Balance */
  477.     *effect=EFX_BALANCE;
  478.     break;
  479.       case EFX_FILTER:            /* ULT: Set vibrato "strength" (depth?)
  480.                                * Not sure if this is correct...
  481.                    */
  482.     *effect=EFX_VIBDEPTH;
  483.     break;
  484.  
  485. #if 0
  486.       /* These effects are not supported by Ultratracker */    
  487.       case EFX_PORTANOTEVOLSLIDE: /* ULT: Special - not supported */
  488.       case EFX_VIBRATOVOLSLIDE:   /* Not supported */
  489.       case EFX_GLISSANDO:
  490.       case EFX_VIBWAVEFORM:
  491.       case EFX_LOOP:
  492.       case EFX_TREMWAVEFORM:
  493. #endif
  494.       default:
  495.     *effect=*arg=0;
  496.     }
  497. }
  498.